/**      
 * @file		RecursiveSelectionSort.cpp
 * @description		This example demonstrates the use of recursion
 *			to sort an array of integers. We've written the
 *			selection sort before with loops.
 * @course		CSCI 123 Section 00000
 * @assignment 
 * @date		mm/dd/yyyy
 * @author		Brad Rippe (00000000) brippe@fullcoll.edu
 * @version		1.0
 */
#include <iostream>
#include <iomanip>
using namespace std;

/**
 * Sorts the array by finding the maximum integer
 * and swapping it with the last element in the array.
 * @param aList the list of integers to sort
 * @param aLimit the last element to sort. This is the
 *			last element to sort. The limit allows us to
 *			reduce the size of the array by one.
 */
void sort(int aList[], int aLimit);

/**
 * Sorts the array but reducing the size of the
 * array by one
 * @param aList the array to list
 */
void sort(int aList[]);

const int SIZE = 10;
/**
 * @return zero if the application executes successfully
 */
int main() {
	
	int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

	cout << "Data items in original order\n";

	for(int i = 0; i < SIZE; i++) {
		cout << setw( 4 ) << a[i];
	}

	sort(a); // sort the array

	cout << "\nData items in ascending order\n";

	for(int j = 0; j < SIZE;j++) {
		cout << setw( 4 ) << a[j];
	}

	cout << endl;
	return 0;
}

void sort(int aList[], int aLimit) {
	if (aLimit > 0) {
		// Find the largest element and its index
		int indexOfMax = 0;
		int max = aList[0];		// assume the first element is the max
		for(int i = 1; i <= aLimit; i++) {
			if (aList[i] > max) {
				max = aList[i];
				indexOfMax = i;
			}
		}

		// Swap the largest with the last element in the list
		aList[indexOfMax] = aList[aLimit];
		aList[aLimit] = max;

		// Sort the remaining list
		sort(aList, aLimit - 1);
	}
}

void sort(int aList[]) {
	sort(aList, SIZE - 1);
}
